﻿using PC_Touch.DataOP;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.Threading;
using HslCommunication;

namespace PC_Touch
{
    public partial class SettingsSF : Form
    {
        public SettingsSF()
        {
            InitializeComponent();
            x = this.Width;
            y = this.Height;
            setTag(this);
            this.Resize += new EventHandler(this.Form_Resize);
            setZH_EN();
        }
        private void setZH_EN()
        {
            if (LanguageHelper.zh_enFlag == "中文")
            {
            }
            else
            {
                uiTitlePanel1.Text = "Motor manual control";
                uiMarkLabel1.Text = "ServoMotor";
                uiMarkLabel2.Text = "BeltTensionCylinder";
                uiMarkLabel9.Text = "Real-timeTensioningForce";
                uiMarkLabel8.Text = "SetSpeed";
                uiMarkLabel7.Text = "ActualSpeed";
                uiMarkLabel6.Text = "Blow-dryingServoSetSpeed";
                uiMarkLabel5.Text = "SetTension";
                uiMarkLabel3.Text = "LeakTestServoSetSpeed";
                uiMarkLabel4.Text = "FunctionTestServoSetSpeed";
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ReadPLCStatuesOnce();
        }
        private void ReadPLCStatuesOnce()
        {
            if (PLCHelper.siemensConnect.IsSuccess)
            {
               // var by = PLCHelper.siemensTcpNet.ReadBool("Q104.4",12).Content;//读取气阀状态
                伺服电机.Active = PLCHelper.siemensTcpNet.ReadBool("DB4.DBX0.0").Content; 
                皮带张紧气缸.Active = PLCHelper.siemensTcpNet.ReadBool("Q101.4").Content;
                
                string[] addresses = new string[] { "DB4.2", "DB4.10", "DB9.46", "DB3.8", "DB3.12", "DB3.16", "DB3.20" };
                OperateResult<byte[]> readResult = PLCHelper.siemensTcpNet.Read(addresses, new ushort[] { 4, 4, 4, 4, 4, 4, 4 });
                if (readResult.IsSuccess)
                {
                    // 解析读取到的数据
                    设定转速.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 0).ToString("F3");
                    ///实际转速.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 4).ToString("F3");
                    ///实时张紧力.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 8).ToString("F3");
                    泄漏测试伺服设定转速.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 12).ToString("F3");
                    功能测试伺服设定转速.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 16).ToString("F3");
                    吹干伺服设定转速.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 20).ToString("F3");
                    设定张力.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 24).ToString("F3");
                }
            }
            else 
            {
                MessageBox.Show("PLC not connect!");
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            TaskManagement taskManagement = new TaskManagement();
            taskManagement.Show();
        }

        #region 控件缩放
        private float x = 1;
        private float y = 1;
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        private void Form_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / x;
            float newy = (this.Height) / y;
            setControls(newx, newy, this);
        }
        #endregion
       
        private void uiSwitch1_ValueChanged(object sender, bool value)
        {
            if (伺服电机.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.18.7", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.18.7", false);
            }
            else 
            {
                PLCHelper.siemensTcpNet.Write("DB10.19.0", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.19.0", false);
            }
        }

        private void uiSwitch4_ValueChanged(object sender, bool value)
        {
            if (皮带张紧气缸.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.4.0", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.4.0", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.4.1", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.4.1", false);
            }
        }       
        private void timerRead_Tick(object sender, EventArgs e)
        {
            if (PLCHelper.siemensConnect.IsSuccess)
            {
                string[] addresses = new string[] { "DB4.2", "DB4.10", "DB9.46","DB3.8","DB3.12","DB3.16","DB3.20" };
                OperateResult<byte[]> readResult = PLCHelper.siemensTcpNet.Read(addresses, new ushort[] {4,4,4,4,4,4,4});
                if (readResult.IsSuccess)
                {
                    // 解析读取到的数据
                    // float value2 = BitConverter.ToSingle(readResult.Content, 4);
                    //设定转速.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 0).ToString("F3");
                    实际转速.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 4).ToString("F3");
                    实时张紧力.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 8).ToString("F3");
                    //泄漏测试伺服设定转速.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 12).ToString("F3");
                   // 功能测试伺服设定转速.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 16).ToString("F3");
                   // 吹干伺服设定转速.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 20).ToString("F3");
                   // 设定张力.Text = PLCHelper.siemensTcpNet.ByteTransform.TransSingle(readResult.Content, 24).ToString("F3");

                }
            }
        }

        private void 设定转速_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                PLCHelper.siemensTcpNet.Write("DB4.2",float.Parse(设定转速.Text));
            }
        }

        private void 设定张力_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                PLCHelper.siemensTcpNet.Write("DB3.20", float.Parse(设定张力.Text));
            }
        }

        private void 泄漏测试伺服设定转速_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                PLCHelper.siemensTcpNet.Write("DB3.8", float.Parse(泄漏测试伺服设定转速.Text));
            }
        }

        private void 功能测试伺服设定转速_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                PLCHelper.siemensTcpNet.Write("DB3.12", float.Parse(功能测试伺服设定转速.Text));
            }
        }
        private void 吹干伺服设定转速_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                PLCHelper.siemensTcpNet.Write("DB3.16", float.Parse(吹干伺服设定转速.Text));
            }
        }
    }
}
